# This function checks for if a layerTableCsv exists. If not it searches recursively 
# (if recursive=TRUE) for shapefiles and geoTiffs from the specified dataPath. It creates
# a new layerTableCsv and adds rows for each of the discovered GIS files. This makes for
# a good starting point for setting up information for loading into the GeoServer.
# 
# The first time this function is run it will scan the directory for GIS files and save
# them in the layerTableCsv. The user can then complete this CSV file. When this is done if
# quickStart is run again it will prompt the user if they wish to upload the GeoServer.
# promptUser	If TRUE then the user will be 
quickStart <- function(layerTableCsv='uploadLayers.csv', dataPath='.', recursive=TRUE, workspace='ea') {
	library("tools"); 	# built in library. file_path_sans_ext

	# No layer table file so create one from GIS files discovered in the current directory
	shapeFiles <- list.files(dataPath, pattern="\\.shp$", recursive=TRUE)
	geoTiffFiles <- list.files(dataPath, pattern="\\.tif$", recursive=TRUE)

	
	allFiles <- c(shapeFiles, geoTiffFiles)
	
	storeType <- c(rep("Shapefile", length(shapeFiles)), rep("GeoTiff", length(geoTiffFiles)))
	
	# TODO: look in the shapefiles and determine if it is a point or polygon. Eventually look at the
	# attribute and create styles for them.
	styles <- c(rep("polygon,line,point", length(shapeFiles)), rep("raster", length(geoTiffFiles)))
	layerName <- paste(gsub(" ","-", gsub(" - ", "_",file_path_sans_ext(basename(allFiles)))), sep="")
	uploadData <- TRUE
	uploadMetadata <- TRUE
	#print(paste(dataPath,"/",allFiles,sep=""))
	layerTableCsv.csv <- data.frame(
		storePath=paste(dataPath,"/",allFiles,sep=""),
		storeName=layerName,
		layerName=layerName,
		workspace=workspace,
		storeType=storeType,
		title=paste("Region: ",file_path_sans_ext(basename(allFiles))," (custodian)"),
		abstract="This layer shows ...",
		metadataXmlHref="http://mydomain.com/geonetwork/srv/en/iso19139.xml?uuid=9fdeb5b3-b407-49e8-ba71-cb10eb31615b",
		keywords="keyword1, keyword2",
		wmsPath=paste("ISO Topic/",dirname(allFiles)),
		styles=styles,
		uploadData=uploadData,
		uploadMetadata=uploadMetadata)
	write.csv(layerTableCsv.csv, layerTableCsv, row.names=FALSE)
	print(paste(length(shapeFiles), " shapefiles and ", length(geoTiffFiles), " were found and saved in ", layerTableCsv, sep=""))
}